<style>
.detailMenu{padding:8px 0px 0px; text-align:right;}
.detailMenuBottom{padding:10px 0px; text-align:right;}
.printArea hr{margin:10px 0px;}
.printArea table{border-collapse: collapse; width:100%;}
.printArea table td{line-height:24px;height:24px;}
.printArea .title{font-size:18px;text-align:center;}

.printArea .part1 td{text-align:left; padding-left:15px;}
.printArea .part1 td.tit{text-align:right;font-weight:bold;}
.printArea .sizeNum{float:left;line-height:24px;margin-left:15px;}

.printArea .part2 td{text-align:center;line-height:24px;height:24px;}
.printArea .part2 td.tit{font-weight:bold;}

.printArea .part3 td{text-align:left; padding-left:15px;}
.printArea .part3 td.tit{text-align:right;font-weight:bold;}
</style>

<style media="print">
.detailMenu {
	display: none;
}
@("@")page
{
    size:  auto;   /* auto is the initial value */
    margin: 0mm;  /* this affects the margin in the printer settings */
}
</style>

<div class="pageContent" layoutH="5">
	<div class="detailMenu">
		{if $isPrint == 1 }
			<a href="javascript:window.print()" class="button"><span>打印</span></a>
			<script>window.print()</script>
		{else /}
			<a href="javascript:printDistributing()" class="layui-btn layui-btn-primary"><span>打印</span></a>
			{if $dInfo['status'] < 4}
			<a href="javascript:void(0)" class="layui-btn layui-btn-normal save-distributing"><span>保存</span></a>
			{/if}
		{/if}
	</div>
	
	<form id="distributingForm">
	<div class="printArea">
		<div class="title">缝前配料清单列表</div>
		<hr />
		<table border="0" cellpadding="0" cellspacing="0" class="part1">
		  <tr>
		    <td width="20%" class="tit">订单号</td>
		    <td width="20%"><span class="copy-item ordernumber">{$dInfo['client_ordernum']}</span></td>
		    <td width="20%" class="tit">裁剪日期</td>
		    <td width="20%">{:date("Y-m-d", $dInfo['create_time'])}</td>
		    <td rowspan="3"><img src="{$pic}" style="max-height:100px;"></td>
		  </tr>
		  <tr>
		    <td class="tit">SKU</td>
		    <td><span class="copy-item">{$oInfo['sku']}</span></td>
		    <td class="tit">货期</td>
		    <td>{:date("Y-m-d", $oInfo['shipment_time'])}</td>
		  </tr>
		  <tr>
		    <td class="tit">订单工厂</td>
		    <td><strong>{$factory_name}</strong></td>
		    <td class="tit">&nbsp;</td>
		    <td>&nbsp;</td>
		  </tr>
		  <tr>
		    <td style="line-height:24px;height:44px;" class="tit">尺寸<br>裁剪数</td>
		    <td colspan="3">
		    	{foreach $osList as $sv }
				<span class="sizeNum">{$sv['size_name']}<br>{$sv['shicai_count']}</span>
				{/foreach}
				<span class="sizeNum">总数<br>{$shicaiCount}</span>
			</td>
		  </tr>
		</table>
  <hr />
		
		<table border="0" class="part2">
		  <tr>
		    <td class="tit">物料名称</td>
		    <td class="tit">物料SKU</td>
		    <td class="tit">码数</td>
		    <td class="tit">颜色</td>
		    <td class="tit">单用量/件</td>
		    <td class="tit">需用量</td>
            <td class="tit">总用量</td>
            <td class="tit">净用量</td>
		    <td class="tit">工厂现配量</td>
		    <td class="tit">已使用料</td>
		    <td class="tit">实际配量</td>
		    <td class="tit">备注</td>
			<td class="tit">标记</td>
		  </tr>
<!--          <input class="customerDosageEdit" type="text" data-mid="$m['mid']" data-order-id="$m['order_id']" value="$m['customer_dosage']"  style="border:none;border-bottom:1px solid gray;backgroup:none;width:40px;">-->
		  {foreach $omList as $m }
			  <tr>
			    <td><span class="copy-item">{$m['mInfo']['name']}</span></td>
			    <td><span class="copy-item">{$m['mInfo']['material_sku']}</span></td>
			    <td>通用</td>
			    <td>{$m['mInfo']['color']}</td>
			    <td>{$m['customer_dosage']}</td>
			    <td>{$m['totalNum']}</td>
			    <td>{$m['total_material_dosage']}</td>
                <td>{$m['jing_material_dosage']}</td>
                <td>{$m['stockNum']}</td>
			    <td>{$m['useNum']}</td>
	
				{if $isPrint == 0 }
				    <td><input type="text" name="allot_num[{$m['material_id']}]" value="{$m['allotNum']}" biggest="{:$m['totalNum']+$m['totalNum']*($m['loss']+2)/100}" materialName="{$m['mInfo']['name']}" totalNum="{$m['totalNum']}" stockNum="{$m['stockNum']}" useNum="{$m['useNum']}" style="border:none;border-bottom:1px solid gray;backgroup:none;width:40px;"></td>
			    {else /}
			    	<td>&nbsp;</td>
			    {/if}
	
			    <td>&nbsp;</td>
				<td><span style="color:red;">{$m['tag']}</span></td>
			  </tr>
		  {/foreach}
		</table>
		
		<p>&nbsp;</p>
		<hr>
		
		<table border="0" class="part3">
		  <tr>
		    <td class="tit">配料员</td>
		    <td>
		    	{if $dInfo['status'] < 4}
			    	<input type="text" name="distribute_user_name" value="{:$dInfo['distribute_user_name']}" style="border:none;border-bottom:1px solid gray;backgroup:none;width:100px;">
		    	{else /}
		    		{:$dInfo['distribute_user_name']}
		    	{/if}
		    </td>
		    <td class="tit">收货人</td>
		    <td>
		    	{if $dInfo['status'] < 4}
			    	<input type="text" name="receipt_user_name" value="{:$dInfo['receipt_user_name']}" style="border:none;border-bottom:1px solid gray;backgroup:none;width:100px;">
		    	{else /}
		    		{:$dInfo['receipt_user_name']}
		    	{/if}
		    </td>
		  </tr>
		  <tr>
		    <td class="tit">配料日期</td>
		    <td><input type="text" readonly name="distribute_time" id="distribute_time" value="{:mimoDate($dInfo['distribute_time'],'Y-m-d')}" style="border:none;border-bottom:1px solid gray;backgroup:none;width:100px;"></td>
		    <td class="tit">收货日期</td>
		    <td><input type="text" readonly name="receipt_time" id="receipt_time" value="{:mimoDate($dInfo['receipt_time'],'Y-m-d')}" style="border:none;border-bottom:1px solid gray;backgroup:none;width:100px;"></td>
		  </tr>
		</table>
	</div>
	<div class="detailMenuBottom">
		{if (($isPrint == 0) && ($dInfo['status'] < 4)) }
			<a href="javascript:void(0)" class="layui-btn over-distributing"><span>配料完成</span></a>
		{/if}
	</div>
    	<input type="hidden" name="id" value="{$id}">
	</form>
</div>

{include file="system@block/layui" /}

<script>
	function printDistributing() {
		window.open("{:url('detail')}?id={$id}&is_print=1&hisi_iframe=yes");
	}

	layui.use(['layer', 'jquery', 'laydate', 'tool'], function() {
		var layer = layui.layer;
		var $ = layui.jquery;
		var laydate = layui.laydate;
		
		{if $dInfo['status'] < 4}
		laydate.render({
		    elem: '#distribute_time'
		    ,theme: 'molv'
		    ,mark: {'{:$today}':''}
		    ,trigger: 'click'
		});
		laydate.render({
		    elem: '#receipt_time'
		    ,theme: 'molv'
		    ,mark: {'{:$today}':''}
		    ,trigger: 'click'
		});
		{/if}
		
		// 保存基本信息
		$(document).on('click','.save-distributing',function(){
			$.ajax({
			   type: "POST",
			   url: "{:url('detail')}?id={$id}&action=saveinfo",
			   data: $("#distributingForm").serialize(),
			   dataType:"json",
			   success: function(res){
				   if(res.code == 1){
					   layer.msg(res.msg, {icon: 1}); 
					   parent.addEditIcon( $(".ordernumber").html() );
					   setTimeout("parent.layui.layer.closeAll()",500);
				   }else layer.msg(res.msg, {icon: 2,anim: 6});
			   }
			});
		});
		
		// 配料完成提交
		window.distributingOverSubmit = function(){
			$.ajax({
			   type: "POST",
			   url: "{:url('detail')}?id={$id}&action=over",
			   data: $("#distributingForm").serialize(),
			   dataType:"json",
			   success: function(res){
				   if(res.code == 1){
					   //parent.window.getOrders();
					   layer.msg(res.msg, {icon: 1});//window.location.reload();
					   setTimeout("parent.layui.layer.closeAll()",900);
					   parent.window.getOrders();
				   }else {
					   layer.msg(res.msg, {icon: 2,anim: 6});
				   }
			   }
			});
		}
		$('.customerDosageEdit').on('change',function(){ 
           var val=$(this).val(),
           mid=$(this).attr("data-mid"),
           order_id=$(this).attr("data-order-id");
            $.ajax({
              type: "POST",
              url: "{:url('customerDosageEdit')}",
              data: "mid="+mid+"&customer_dosage="+val+"&order_id="+order_id,
              dataType:"json",
              success: function(res){
                 if (res.code == 1) {
                    layer.msg(res.msg, {icon: 1});
                 } else {
                    layer.msg(res.msg, {icon: 2,anim: 6});
                 }
              }
           });
       });
		// 配料完成
		$(document).on('click','.over-distributing',function(){
			
			var isError = false;
			var warnMsg = "";
			$("input[name^='allot_num']").each(
				function() {
					allotNum = $(this).val();
					materialName = $(this).attr("materialName");
					totalNum = parseFloat($(this).attr("totalNum"));
					stockNum = parseFloat($(this).attr("stockNum"));
					useNum = parseFloat($(this).attr("useNum"));
                    biggest=parseFloat($(this).attr("biggest"));
					if (allotNum == '') {
						isError = true;
						layer.msg("请填写材料 ["+materialName+"] 的实际配量", {icon: 2,anim: 6});
						return;
					}
					if ((allotNum-useNum) > stockNum) {
						//isError = true;
						//layer.msg("材料 ["+materialName+"] 库存不足", {icon: 2,anim: 6});
						//return;
					}
                    
					if ((allotNum-totalNum)/totalNum > 0.15) {
						//isError = true;
						//layer.msg("材料 ["+materialName+"] 配量超标", {icon: 2,anim: 6});
					}

					if (allotNum/totalNum < 0.85) {
						warnMsg=warnMsg+"材料 ["+materialName+"] 配量不足<br>";
					}
                    if(allotNum>=biggest || allotNum<0){
                        //isError = true;
						//layer.msg("材料 ["+materialName+"] 辅料配量超量", {icon: 2,anim: 6});
                    }
				}
			);

			if (isError) return false;
			if (warnMsg != '') {
				layer.confirm(warnMsg, {btn: ['确认','取消']}, function(){
						distributingOverSubmit();
					});
			} else {
				setTimeout(function(){
					distributingOverSubmit();
				},200);
			}

		});
	});
		
</script>
